package sys

import (
	"context"
	"fmt"
	"github.com/google/wire"
	"sieve_admin_server_userpass/app/dao"
	"sieve_admin_server_userpass/app/dao/notice"
	"sieve_admin_server_userpass/app/dao/user"
	"sieve_admin_server_userpass/app/schema"
	"strconv"
	"time"
)

var (
	NoticeSet = wire.NewSet(wire.Struct(new(NoticeSrv), "*"))
)

type NoticeSrv struct {
	NoticeRepo *dao.NoticeRepo
}

func (a *NoticeSrv) Create(ctx context.Context, item map[string]interface{}) interface{} {

	var (
		now = time.Now().Unix()
		def = map[string]interface{}{
			"create_at":  now,
			"updated_at": now,
		}
	)

	for k, v := range def {
		item[k] = v
	}

	return a.NoticeRepo.Create(ctx, item)
}

func (a *NoticeSrv) Query(ctx context.Context, reqArgs map[string]interface{}, p schema.PaginationParam) interface{} {

	type columnType struct {
		ID          int64   `json:"id"`        //  自增id
		MenuName    string  `json:"menu_name"` //  项目菜单（指的是菜单名称）
		Title       string  `json:"title"`     //  标题说明（指的是菜单栏名称界面里的子功能说明）
		Content     string  `json:"content"`   //  展示内容（提示信息内容）
		Status      int64   `json:"status"`    //  状态（1:启用，2：禁用）
		Creator     int64   `json:"creator"`   //  创建人（修改人）
		CreatorName *string `json:"creator_name"`
		CreateAt    int64   `json:"create_at"`
		UpdatedAt   int64   `json:"updated_at"`
		Deleted     int64   `json:"deleted"` //  标识记录是否被删除（0：没有被删除，1：被删除）
	}

	var (
		noticeTableName = notice.Notice{}.TableName()
		userTableName   = user.User{}.TableName()
		joinString      = fmt.Sprintf(
			`
			LEFT JOIN %v ON %v.id = %v.creator and %v.deleted = 0`,
			userTableName,
			userTableName,
			noticeTableName,
			userTableName,
		)
		repKeys = []string{
			fmt.Sprintf("%v.id as id", noticeTableName),
			fmt.Sprintf("%v.menu_name as menu_name", noticeTableName),
			fmt.Sprintf("%v.title as title", noticeTableName),
			fmt.Sprintf("%v.content as content", noticeTableName),
			fmt.Sprintf("%v.status as status", noticeTableName),
			fmt.Sprintf("%v.creator as creator", noticeTableName),
			fmt.Sprintf("%v.account as creator_name", userTableName),
			fmt.Sprintf("%v.create_at as create_at", noticeTableName),
			fmt.Sprintf("%v.updated_at as updated_at", noticeTableName),
		}
	)

	ctx = context.WithValue(
		context.WithValue(ctx, "join", joinString),
		"column_types", columnType{},
	)

	req, err := a.NoticeRepo.Query(ctx, reqArgs, p, repKeys)
	if err != nil {
		return err
	}
	return req
}

func (a *NoticeSrv) Enable(ctx context.Context, reqArgs map[string]interface{}) error {

	var (
		now        = time.Now().Unix()
		updateKeys = map[string]interface{}{
			"updated_at": now,
			"status":     1,
		}
		queryKeys = map[string]interface{}{
			"id": reqArgs["id"],
		}
	)

	return a.NoticeRepo.Update(ctx, updateKeys, queryKeys)
}

func (a *NoticeSrv) Disable(ctx context.Context, reqArgs map[string]interface{}) error {

	var (
		now        = time.Now().Unix()
		updateKeys = map[string]interface{}{
			"updated_at": now,
			"status":     2,
		}
		queryKeys = map[string]interface{}{
			"id": reqArgs["id"],
		}
	)

	return a.NoticeRepo.Update(ctx, updateKeys, queryKeys)
}

func (a *NoticeSrv) Update(ctx context.Context, reqArgs map[string]interface{}) error {

	var (
		now        = time.Now().Unix()
		updateKeys = map[string]interface{}{
			"updated_at": now,
			"menu_name":  reqArgs["menu_name"],
			"title":      reqArgs["title"],
			"content":    reqArgs["content"],
		}
		queryKeys = map[string]interface{}{
			"id": reqArgs["id"],
		}
	)

	return a.NoticeRepo.Update(ctx, updateKeys, queryKeys)
}

func (a *NoticeSrv) Delete(ctx context.Context, reqArgs map[string]interface{}) interface{} {

	var (
		id, _ = strconv.ParseUint(fmt.Sprintf("%v", reqArgs["id"]), 10, 64)
	)

	return a.NoticeRepo.SoftDelete(ctx, id)
}
